package academic.Impl;

import academic.dao.WcqAcademicDao;
import academic.domain.WcqAcademic;
import achievement.dao.SzlBookDao;
import achievement.domain.SzlBook;
import cn.hutool.core.util.StrUtil;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class WcqAcademicDaoImpl implements WcqAcademicDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;

    @Override
    public void insert(WcqAcademic wcqAcademic) {
        String sql = "insert into wcq_academic\n" +
                "        (activity_id,user_id,activity_name,activity_photo,activity_addr,activity_time,activity_info,activity_status1,activity_status1_time,activity_status2,activity_status2_time,activity_status,activity_comment) \n" +
                "values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,wcqAcademic.getActivity_id());
            psmt.setLong(2,wcqAcademic.getUser_id());
            psmt.setString(3,wcqAcademic.getActivity_name());
            psmt.setString(4,wcqAcademic.getActivity_photo());
            psmt.setString(5,wcqAcademic.getActivity_addr());
            psmt.setString(6,wcqAcademic.getActivity_time());
            psmt.setString(7,wcqAcademic.getActivity_info());
            psmt.setInt(8,wcqAcademic.getActivity_status1());
            psmt.setString(9,wcqAcademic.getActivity_status1_time());
            psmt.setInt(10,wcqAcademic.getActivity_status2());
            psmt.setString(11,wcqAcademic.getActivity_status2_time());
            psmt.setInt(12,wcqAcademic.getActivity_status());
            psmt.setString(13,wcqAcademic.getActivity_comment());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void updateById(WcqAcademic wcqAcademic) {
        String sql = "update wcq_academic set";
        List<Object> param = new ArrayList<>();
        if (wcqAcademic == null){
            return;
        }else{
            if(wcqAcademic.getActivity_id() != null){
                sql += " activity_id = ?,";
                param.add(wcqAcademic.getActivity_id());
            }
            if(wcqAcademic.getUser_id() != null){
                sql += " user_id = ?,";
                param.add(wcqAcademic.getUser_id());
            }
            if(wcqAcademic.getActivity_name() != null){
                sql += " activity_name = ?,";
                param.add(wcqAcademic.getActivity_name());
            }
            if(wcqAcademic.getActivity_photo() != null){
                sql += " activity_photo = ?,";
                param.add(wcqAcademic.getActivity_photo());
            }
            if(wcqAcademic.getActivity_addr() != null){
                sql += " activity_addr = ?,";
                param.add(wcqAcademic.getActivity_addr());
            }
            if(wcqAcademic.getActivity_time() != null){
                sql += " activity_time = ?,";
                param.add(wcqAcademic.getActivity_time());
            }
            if(wcqAcademic.getActivity_info() != null){
                sql += " activity_info = ?,";
                param.add(wcqAcademic.getActivity_info());
            }
            if(wcqAcademic.getActivity_status1() != null){
                sql += " activity_status1 = ?,";
                param.add(wcqAcademic.getActivity_status1());
            }
            if(wcqAcademic.getActivity_status1_time() != null){
                sql += " activity_status1_time = ?,";
                param.add(wcqAcademic.getActivity_status1_time());
            }
            if(wcqAcademic.getActivity_status2() != null){
                sql += " activity_status2 = ?,";
                param.add(wcqAcademic.getActivity_status2());
            }
            if(wcqAcademic.getActivity_status2_time() != null){
                sql += " activity_status2_time = ?,";
                param.add(wcqAcademic.getActivity_status2_time());
            }
            if(wcqAcademic.getActivity_status() != null){
                sql += " activity_status = ?,";
                param.add(wcqAcademic.getActivity_status());
            }
            if(wcqAcademic.getActivity_comment() != null){
                sql += " activity_comment = ?,";
                param.add(wcqAcademic.getActivity_comment());
            }

        }
        sql = StrUtil.strip(sql,null,",");
        sql+=" where activity_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            psmt.setLong(param.size()+1,wcqAcademic.getActivity_id());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void deleteById(Long activity_id) {
        String sql = "delete from wcq_academic where activity_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,activity_id);
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public WcqAcademic selectById(Long activity_id) {
        String sql = "select * from wcq_academic where activity_id = ?";
        WcqAcademic wcqAcademic = new WcqAcademic();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,activity_id);
            rs = psmt.executeQuery();
            while (rs.next()){
                wcqAcademic.setActivity_id(rs.getLong(1));
                wcqAcademic.setUser_id(rs.getLong(2));
                wcqAcademic.setActivity_name(rs.getString(3));
                wcqAcademic.setActivity_photo(rs.getString(4));
                wcqAcademic.setActivity_addr(rs.getString(5));
                wcqAcademic.setActivity_time(rs.getString(6));
                wcqAcademic.setActivity_info(rs.getString(7));
                wcqAcademic.setActivity_status1(rs.getInt(8));
                wcqAcademic.setActivity_status1_time(rs.getString(9));
                wcqAcademic.setActivity_status2(rs.getInt(10));
                wcqAcademic.setActivity_status2_time(rs.getString(11));
                wcqAcademic.setActivity_status(rs.getInt(12));
                wcqAcademic.setActivity_comment(rs.getString(13));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return wcqAcademic;
    }

    @Override
    public List<WcqAcademic> selectList(WcqAcademic wcqAcademic) {
        return null;
    }
    @Override
    public  List<WcqAcademic> selectByUserId(Long user_id)
    {
        String sql = "select * from wcq_academic where user_id = ?";
        WcqAcademic wcqAcademic = new WcqAcademic();
        List<WcqAcademic> WcqAcademicList=new ArrayList<>();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);

            psmt.setLong(1,user_id);
            rs = psmt.executeQuery();
            while (rs.next()){
                wcqAcademic.setActivity_id(rs.getLong(1));
                wcqAcademic.setUser_id(rs.getLong(2));
                wcqAcademic.setActivity_name(rs.getString(3));
                wcqAcademic.setActivity_photo(rs.getString(4));
                wcqAcademic.setActivity_addr(rs.getString(5));
                wcqAcademic.setActivity_time(rs.getString(6));
                wcqAcademic.setActivity_info(rs.getString(7));
                wcqAcademic.setActivity_status1(rs.getInt(8));
                wcqAcademic.setActivity_status1_time(rs.getString(9));
                wcqAcademic.setActivity_status2(rs.getInt(10));
                wcqAcademic.setActivity_status2_time(rs.getString(11));
                wcqAcademic.setActivity_status(rs.getInt(12));
                wcqAcademic.setActivity_comment(rs.getString(13));
                WcqAcademicList.add(wcqAcademic);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return WcqAcademicList;
    }
}
